/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao.mysql;

import dao.CustomerDAO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import model.Customer;

/**
 *
 * @author Student
 */
public class CustomerDAOImpl implements CustomerDAO {

    private Connection conn;

    public CustomerDAOImpl(Connection conn) {
        this.conn = conn;
    }

    @Override
    public int save(Customer customer) throws Exception {
        String sql = "INSERT INTO customer(nama,alamat,telp) VALUES(?,?,?)";
        int out = 0;
        PreparedStatement ps = null;
        try {
            ps = this.conn.prepareStatement(sql);
            ps.setString(1, customer.getNama());
            ps.setString(2, customer.getAlamat());
            ps.setString(3, customer.getTelp());
            out = ps.executeUpdate();
            if (ps != null) {
                ps.close();
            }
        } catch (Exception ex) {
            throw ex;
        }
        return out;
    }

    @Override
    public int update(Customer customer) throws Exception {
        String sql = "UPDATE customer SET nama=?,alamat=?,telp=? WHERE id=?";
        int out = 0;
        PreparedStatement ps = null;
        try {
            ps = this.conn.prepareStatement(sql);
            ps.setString(1, customer.getNama());
            ps.setString(2, customer.getAlamat());
            ps.setString(3, customer.getTelp());
            ps.setInt(4, customer.getId());
            out = ps.executeUpdate();
            if (ps != null) {
                ps.close();
            }
        } catch (Exception ex) {
            throw ex;
        }
        return out;
    }

    @Override
    public int delete(int id) throws Exception {
        String sql = "DELETE FROM customer WHERE id=?";
        int out = 0;
        PreparedStatement ps = null;
        try {
            ps = this.conn.prepareStatement(sql);
            ps.setInt(1, id);
            out = ps.executeUpdate();
            if (ps != null) {
                ps.close();
            }
        } catch (Exception ex) {
            throw ex;
        }
        return out;
    }

    @Override
    public Customer getById(int id) throws Exception {
        String sql = "select id,nama,alamat,telp from customer where id=?";
        Customer cust = null;
        PreparedStatement ps = null;
        try {
            ps = this.conn.prepareStatement(sql);
            ps.setInt(1, id);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                cust = new Customer();
                cust.setId(rs.getInt(1));
                cust.setNama(rs.getString(2));
                cust.setAlamat(rs.getString(3));
                cust.setTelp(rs.getString(4));
            }
            if (ps != null) {
                ps.close();
            }
        } catch (Exception ex) {
            throw ex;
        }
        return cust;
    }

    @Override
    public List<Customer> getAll() throws Exception {
        String sql = "select id,nama,alamat,telp from customer";
        List<Customer> customers = new ArrayList<Customer>();
        PreparedStatement ps = null;
        try {
            ps = this.conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                Customer cust = new Customer();
                cust.setId(rs.getInt(1));
                cust.setNama(rs.getString(2));
                cust.setAlamat(rs.getString(3));
                cust.setTelp(rs.getString(4));
                customers.add(cust);
            }
            if (ps != null) {
                ps.close();
            }
        } catch (Exception ex) {
            throw ex;
        }
        return customers;
    }
}
